package jbl.io.excel;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.ServletActionContext;

public class ExcelTool {
	
	public static Workbook createWorkBook(InputStream is, String filename) throws IOException{ 
        if(filename.toLowerCase().endsWith("xls")){ 
            return new HSSFWorkbook(is); 
        } 
        if(filename.toLowerCase().endsWith("xlsx")){ 
            return new XSSFWorkbook(is); 
        } 
        return null;
    } 
	
	public static List<FileItem> getItemFile() throws FileUploadException{
		HttpServletRequest req = ServletActionContext.getRequest();
		String path = req.getSession().getServletContext().getRealPath("/");
		DiskFileItemFactory fac =new DiskFileItemFactory();
		fac.setRepository(new File(path));
		fac.setSizeThreshold(1024*1024*10);
		ServletFileUpload sf = new ServletFileUpload(fac);
		sf.setSizeMax(1024*1024*200);
		return sf.parseRequest(req);
		
	}
	
	public static String toNyStr(String str){
		if(!"null".equals(str) && !"".equals(str)){
			return str;
		}
		return "";
	}

}
